package cn.edu.tsinghua.training.cms.demo.util;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Scanner;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import freemarker.template.Template;

public class GenerateSource {

	private final static String PACKAGE = "cn.edu.tsinghua.training.cms.demo";
	private final static String JAVA_PATH = "src/main/java/";
	private static final String JSP_PATH = "src/main/webapp/";
	private final static String PATH = JAVA_PATH + PACKAGE.replace(".", "/");
	private final static String[] JAVA_TPLS = {"Entity", "Dao.Impl", "Service", "Service.Impl", "Action"};
	private final static String[] JSP_TPLS = {"add", "list", "edit"};
	private static String entity;
	private static String pk;
	private static String properties;
	private static Scanner scanner = new Scanner(System.in);
	private static ConfigurableApplicationContext configurableApplicationContext = new ClassPathXmlApplicationContext("beans/freemarker.xml");

	private static void generate(String tpl, String type) throws Exception {
		Hashtable<String, String> hashtable = new Hashtable<String, String>();
		for (String keyvalue : properties.split(", ")) {
			String[] strings = keyvalue.split(" ");
			hashtable.put(strings[1], strings[0]);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("properties", hashtable);
		map.put("package", PACKAGE);
		map.put("entity", entity);
		map.put("PK", pk);
		File file = null;
		String name = null;
		if (type.equals("java")) {
			file = new File(PATH + "/" + tpl.toLowerCase().replace('.', '/') + "/" + entity + tpl.replace(".", "") + ".java");
			if (tpl.equals("Entity")) {
				file = new File(PATH + "/entity/" + entity + ".java"); 
			}
			name = "java/" + tpl + ".ftl";
		} else if (type.equals("jsp")) {
			File path = new File(JSP_PATH + entity.toLowerCase());
			if (!path.exists()) {
				path.mkdir();
			}
			file = new File(path + "/" + entity.toLowerCase() + "_" + tpl + ".jsp");
			name = "jsp/" + tpl + ".ftl";
		}
		Writer writer = new FileWriter(file);
		FreeMarkerConfig freeMarkerConfig = (FreeMarkerConfig) configurableApplicationContext.getBean("freeMarkerConfigurer");
		Template template = freeMarkerConfig.getConfiguration().getTemplate(name);
		template.process(map, writer);
		writer.close();
		System.out.println("\t" + file + " \t generated!");
	}
	
	public static void input() {
		System.out.println("input entity class name, e.g:User:");
		entity = scanner.nextLine();
		System.out.println("input primary key type, e.g:Integer:");
		pk = scanner.nextLine();
		System.out.println("input properties, e.g:String username, String password, ...:");
		properties = scanner.nextLine();
	}

	public static void main(String[] args) throws Exception {
		input();
		for (String tpl : JAVA_TPLS) {
			generate(tpl, "java");
		}
		for (String tpl : JSP_TPLS) {
			generate(tpl, "jsp");
		}
		System.out.println("refresh project.");
		scanner.close();
		configurableApplicationContext.close();
	}
	
}
